/* 
Scenic Graphics Library
Copyright (C) 2007 Jouni Tulkki

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USApackage scenic;
*/

#include <stdio.h>
#include <stdlib.h>

#include "common.h"


using namespace Scenic::Renderer;


//
// class Color
//

int RGBAFloat::getRGBA32() const
{
#ifdef DIRECTX
	return ((int)(red * 255) << 16) |
		((int)(green * 255) << 8) |
		((int)(blue * 255) << 0) | 
		((int)(alpha * 255) << 24);
#endif
#ifdef OPT_OPENGL
	return ((int)(red * 255) << 0) |
		((int)(green * 255) << 8) |
		((int)(blue * 255) << 16) | 
		((int)(alpha * 255) << 24);
#endif
}

//
// class ClipArea
//

int ClipArea::getModulation()
{
	if(type == SURFACE_TYPE_ALPHA2X)
		return 2;
	if(type == SURFACE_TYPE_ALPHA4X)
		return 4;
	return 1;
}

//
// Affine2
//

Affine2 Affine2::inverse()
{
	Affine2 b;
	double det = m00 * m11 - m01 * m10;
	
	if(det == 0.0)
		return b;

	b.m00 = m11 / det;
	b.m01 = -m01 / det;
	b.dx = -(b.m00 * dx + b.m01 * dy);
	
	b.m10 = -m10 / det;
	b.m11 = m00 / det;
	b.dy = -(b.m10 * dx + b.m11 * dy);
	
	return b;
}

//
// Primitives
//

Primitives::Primitives()
{
	context = NULL;
	vertexStore = new VertexStore();
}

Primitives::~Primitives()
{
	if(vertexStore)
		delete vertexStore;
}

void Primitives::setContext(Context *context) 
{ 
	if(context != this->context)
	{
		flush();
		this->context = context; 
		vertexStore->setContext(context);
	}
}

void Primitives::setVertexType(int type) 
{ 
	if(vertexStore->getType() != type)
	{
		flush();
		vertexStore->setType(type);
	}
}

void Primitives::flush()
{
	if(context && !vertexStore->isEmpty())
	{
		render();
		vertexStore->empty();
	}
}

